/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.csp.sentinel.dashboard.rule.nacos;

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigFactory;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @author Eric Zhao
 * @since 1.4.0
 */
@Configuration
public class NacosConfig {
  @Value("${nacos.serverAddr:localhost:8848}")
  private String serverAddr;
  @Value("${nacos.namespace:DEFAULT_GROUP}")
  private String namespace;
  @Value("${nacos.username}")
  private String username;
  @Value("${nacos.password}")
  private String password;

  @Bean
  public ConfigService nacosConfigService() throws Exception {
    Properties properties = new Properties();
    //nacos地址
    properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
    //namespace为空即为public
    properties.put(PropertyKeyConst.NAMESPACE, namespace);
    if (username != null) {
      properties.put(PropertyKeyConst.USERNAME, username);
      properties.put(PropertyKeyConst.PASSWORD, password);
    }

    return ConfigFactory.createConfigService(properties);
  }
}
